package com.cn.unarmed.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/6/30.
 * 重写用于RecyclerView的线性布局管理器用于在被包含于scrollview的时候重写高度
 */
public class WrapScrollLayoutManager extends LinearLayoutManager {

    public WrapScrollLayoutManager(Context context) {
        super(context);
    }

    public WrapScrollLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    private int[] mMeasuredDimension = new int[2];

    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
                          int widthSpec, int heightSpec) {
        super.onMeasure(recycler, state, widthSpec, heightSpec);
        final int widthModel = View.MeasureSpec.getMode(widthSpec);
        final int heightModel = View.MeasureSpec.getMode(heightSpec);
        final int widthSize = View.MeasureSpec.getSize(widthSpec);
        final int heightSize = View.MeasureSpec.getSize(heightSpec);

        int width = 0, height = 0;
        for (int i = 0; i < getItemCount(); i++) {
            meaSureScrapChild(recycler, i, widthSpec, heightSpec, mMeasuredDimension);
            if (HORIZONTAL == getOrientation()) {
                width = width + mMeasuredDimension[0];
                if (i == 0) {
                    height = mMeasuredDimension[1];
                }
            }
            if (VERTICAL == getOrientation()) {
                height = height + mMeasuredDimension[1];
                if (i == 0){
                    width = mMeasuredDimension[0];
                }
            }
        }
        switch (widthModel){
            case View.MeasureSpec.EXACTLY:
                width = widthSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }
        switch (heightModel){
            case View.MeasureSpec.EXACTLY:
                height = heightSize;
            case View.MeasureSpec.AT_MOST:
            case View.MeasureSpec.UNSPECIFIED:
        }

        setMeasuredDimension(width, height);//设置组件占据的宽高
    }

    private void meaSureScrapChild(RecyclerView.Recycler recycler,int position,
                                   int widthSpec, int heightSpec, int[] meaSureDimention) {
        try {
            View view = recycler.getViewForPosition(0);
            if (view != null) {
                RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
                int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, getPaddingLeft() + getPaddingRight(), p.width);
                int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, getPaddingTop() + getPaddingBottom(), p.height);
                view.measure(childWidthSpec, childHeightSpec);
                meaSureDimention[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
                meaSureDimention[1] = view.getMeasuredHeight() + p.topMargin + p.bottomMargin;
                recycler.recycleView(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
